﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Engine;
using System.Xml.Linq;
using MFTrain;
using Engine.Graphics;

namespace Game
{
    public class MFEngineWidget : CanvasWidget
    {
        public GridPanelWidget m_inventoryGrid;

        public InventorySlotWidget m_fuelSlot;

        public FireWidget m_fire;

        public ComponentMFEngine m_componentFurnace;

        public ComponentMFChestCache componentChestCache;

        public MFWaterWidget waterWidget;

        public GridPanelWidget chestCacheGrid;

        public BitmapButtonWidget breakButton;

        public LabelWidget title;

        public MFEngineWidget(IInventory inventory, ComponentMFEngine componentFurnace, ComponentMFChestCache componentChestCache)
        {
            m_componentFurnace = componentFurnace;
            this.componentChestCache = componentChestCache;

            XElement node = ContentManager.Get<XElement>("MF/Train/EngineWidget");
            LoadContents(this, node);
            m_inventoryGrid = Children.Find<GridPanelWidget>("InventoryGrid");
            m_fire = Children.Find<FireWidget>("Fire");
            m_fuelSlot = Children.Find<InventorySlotWidget>("FuelSlot");
            waterWidget = Children.Find<MFWaterWidget>("Water");
            chestCacheGrid = Children.Find<GridPanelWidget>("CacheChestGrid");
            breakButton = Children.Find<BitmapButtonWidget>("BreakButton");
            title = Children.Find<LabelWidget>("Title");

            breakButton.IsAutoCheckingEnabled = true;

            Subtexture blockTexture = new Subtexture(Texture2D.Load(new Engine.Media.Image(1, 1) { Pixels = { [0] = new Color(16,16,16,128)}}), Vector2.Zero, Vector2.One);
            Subtexture emptyTexture = new Subtexture(Texture2D.Load(new Engine.Media.Image(1, 1) { Pixels = { [0] = Color.Transparent}}), Vector2.Zero, Vector2.One);
            breakButton.ClickedSubtexture = blockTexture;
            breakButton.NormalSubtexture = emptyTexture;
            breakButton.IsChecked = m_componentFurnace.engineBreak;

            //背包区
            int num = 10;
            for (int i = 0; i < m_inventoryGrid.RowsCount; i++)
            {
                for (int j = 0; j < m_inventoryGrid.ColumnsCount; j++)
                {
                    var inventorySlotWidget = new InventorySlotWidget();
                    inventorySlotWidget.AssignInventorySlot(inventory, num++);
                    m_inventoryGrid.Children.Add(inventorySlotWidget);
                    m_inventoryGrid.SetWidgetCell(inventorySlotWidget, new Point2(j, i));
                }
            }

            //蒸汽机
            m_fuelSlot.AssignInventorySlot(componentFurnace, 0);
            for (int i = 0; i < 2; i++)//水槽上的格子
            {
                Vector2 pos = new Vector2(28, 52 + i * 72);
                InventorySlotWidget inventorySlotWidget = new InventorySlotWidget();
                inventorySlotWidget.Margin = pos;
                inventorySlotWidget.BevelColor = Color.Transparent;
                inventorySlotWidget.CenterColor = Color.Transparent;
                inventorySlotWidget.AssignInventorySlot(componentFurnace, 1);
                inventorySlotWidget.HideEditOverlay = true;
                inventorySlotWidget.HideInteractiveOverlay = true;
                inventorySlotWidget.HideFoodOverlay = true;
                inventorySlotWidget.HideHighlightRectangle = true;
                inventorySlotWidget.HideBlockIcon = true;
                Children.Add(inventorySlotWidget);
            }

            //物品缓存区
            num = 0;
            for (int k = 0; k < chestCacheGrid.RowsCount; k++)
            {
                for (int l = 0; l < chestCacheGrid.ColumnsCount; l++)
                {
                    var inventorySlotWidget2 = new InventorySlotWidget();
                    inventorySlotWidget2.AssignInventorySlot(componentChestCache, num++);
                    chestCacheGrid.Children.Add(inventorySlotWidget2);
                    chestCacheGrid.SetWidgetCell(inventorySlotWidget2, new Point2(l, k));
                }
            }
        }

        public override void Update()
        {
            m_componentFurnace.engineBreak = breakButton.IsChecked;

            waterWidget.Color = m_componentFurnace.GetLocalWaterColor();
            waterWidget.ParticlesPerSecond = (!m_componentFurnace.engineBreak && m_componentFurnace.fireTimeRemaining > 0) ? (6 * m_componentFurnace.fuelLevel) : 0;
            waterWidget.Value = m_componentFurnace.waterVolumn;
            m_fire.ParticlesPerSecond = (!m_componentFurnace.engineBreak && m_componentFurnace.fireTimeRemaining > 0) ? (12 * m_componentFurnace.fuelLevel) : 0;
            title.Text = "蒸汽机车" + (m_componentFurnace.engineBreak ? "(制动)" : "");
            if (!m_componentFurnace.IsAddedToProject)
            {
                ParentWidget.Children.Remove(this);
            }
        }
    }
}
